//
//  HotelDetailCtrl.m
//  Traveling
//
//  Created by Northwan on 15/6/7.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//  酒店详情

#import "HotelDetailCtrl.h"
#import "HotelDetailHeaderView.h"
#import "HomeDataTool.h"
#import "HotelRootItemCell.h"
#import "HomeItemDetailHeaderModel.h"
#import "OrderEditCtrl.h"

@interface HotelDetailCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *itemArray;
@property (nonatomic, strong) HotelStayResult *hotelStayResult;
@property (nonatomic, weak) HotelDetailHeaderView *headerView;
@end

@implementation HotelDetailCtrl

-(HotelDetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [HotelDetailHeaderView headerView];
        [_headerView setWidth:SCREENW];
        _headerView.height = 312;
        [_headerView setHotelStayResult:self.hotelStayResult];
    }
    return _headerView;
}

-(instancetype)initWithHotelStayResult:(HotelStayResult *)hotelStayResult
{
    self = [super init];
    if (self) {
        self.hotelStayResult = hotelStayResult;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"房间列表"];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView headerBeginRefreshing];
    self.tableView.rowHeight = 122;
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView registerNib:[UINib nibWithNibName:@"HotelRootItemCell" bundle:nil] forCellReuseIdentifier:@"HotelRootItemCell"];
    
}

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView beginUpdates];
    [self.tableView setTableHeaderView:self.headerView];
    [self.tableView endUpdates];
    [self.headerView setBanner];
}

-(void)headerRefresh{
    [self requestDataWithContentId:self.hotelStayResult.id];
}

-(void)requestDataWithContentId:(NSInteger)contentId{
    [HomeDataTool getHotelRoomListWithContentId:contentId success:^(HotelRoomRes *result) {
        [Utils refreshComplete:self.tableView];
        if (result.isSucc) {
            [self.itemArray removeAllObjects];
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }

    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HotelRootItemCell *cell = [HotelRootItemCell initWithTableView:tableView];
    HotelRoomItemInfo *info =  self.itemArray[indexPath.row];
    [cell setItemInfo:info];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HotelRoomItemInfo *info =  self.itemArray[indexPath.row];
    HomeItemDetailHeaderModel* item  = [[HomeItemDetailHeaderModel alloc]init];
    item.contentId = self.hotelStayResult.id;
    item.type = HotelDetailType;
    [item setTitle:self.hotelStayResult.hotel_name];
//    [item setName:self.hotelStayResult.hotel_name];
    [item setPrePrice:info.price];
    [item setCurrentPrice:info.rateprice];
    [item setCheckInDate:self.headerView.startDate];
    [item setCheckOutDate:self.headerView.endDate];
    [item setIconUrl:info.cover_img];
    [item setNetwork:info.broadband];
    [item setBreakfast:info.breakfast];
    [item setRoomType:info.typename];
    [item setTotalNight:self.headerView.dayCount];
    [item setBedType:info.bedname];
    [item setRoomId:info.id];
    OrderEditCtrl *ctrl = [[OrderEditCtrl alloc]initWithData:item type:HotelStayOrderEditType];
    [self.navigationController pushViewController:ctrl animated:YES];
}

//-(BOOL)shouldAdjustEdge{
//    return NO;
//}

@end
